package com.model;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Perpustakaan {

    public static String NAMA = "Kaito Library";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();

    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static void simpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314035", "MHS115314035");
            Statement statement;
                try     {statement = conn.createStatement(); } 
                catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                String sql = "insert into publikasi (id_koleksi, judul) values ('" + DAFTAR_PUBLIKASI.get(i).getIdKoleksi() + "','" + DAFTAR_PUBLIKASI.get(i).getJudul() + "')";
                try     {statement = conn.createStatement(); 
                         statement.executeUpdate(sql); }
                catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
            }
                try     {conn.close(); }
                catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
        }
    }

    public static void bacaTabelPublikasi() {
        DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314035", "MHS115314035");
        Statement statement;
            try     {statement = conn.createStatement(); }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
            try     {statement = conn.createStatement(); }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
        String sql = "select * from publikasi";
            try     {statement = conn.createStatement(); 
                     ResultSet result = statement.executeQuery(sql); 
                     result.next(); 
                     
            }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);   
            }
    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            try {
                File outFile = new File("Publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void tambahTabelPublikasi(Publikasi publikasi){
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314035", "MHS115314035");
        Statement statement;
            try     {statement = conn.createStatement(); }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
        String sql = "insert into publikasi (id_koleksi, judul) values ('"+publikasi.getIdKoleksi() + "','" + publikasi.getJudul()+"')";
            try     {statement = conn.createStatement();
                     statement.executeUpdate(sql); }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex); }
            try     {conn.close(); }
            catch   (SQLException ex) {Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void bacaFilePublikasi() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("Publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            DAFTAR_PUBLIKASI = (ArrayList<Publikasi>) inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
            }
            return hasil;
        }
        return "Kosong";
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += DAFTAR_ANGGOTA.get(i).getNama() + "\n";
            }
            return hasil;
        }
        return "Kosong";
    }
    
    public static ArrayList<Anggota> cariAnggota(String nama){
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for(int i=0; i<DAFTAR_ANGGOTA.size(); i++){
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if(temp.getNama().contains(nama)){
            hasil.add(temp); }
        }
        return hasil;
    }    
}